import Vue from 'vue';
import VueRouter from 'vue-router';
import { iftest } from '@/env.js';
import * as dd from 'dingtalk-jsapi';

var ua = window.navigator.userAgent.toLowerCase();
var wx = false;

if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  // alert('是微信打开的')
  wx = true;
} else {
  // alert('不微信打开的')
  wx = false;
}

// alert(wx)

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: dd.env.platform !== 'notInDingTalk' ? '/ding' : wx ? '/ding' : '/index'
    // redirect: '/ding'
  },

  {
    path: '/dp',
    name: 'dp',
    component: (resolve) => require(['../views/dp.vue'], resolve)
  },
  {
    path: '/day',
    name: 'day',
    component: (resolve) => require(['../views/day.vue'], resolve)
  },
  {
    path: '/reg',
    name: 'reg',
    component: (resolve) => require(['../views/reg.vue'], resolve)
  },
  {
    path: '/login',
    name: 'login',
    component: (resolve) => require(['../views/login.vue'], resolve)
  },
  {
    path: '/ding',
    name: 'ding',
    component: (resolve) => require(['../views/dingDing.vue'], resolve)
  },
  {
    path: '/index',
    name: 'index',
    component: (resolve) => require(['../views/index.vue'], resolve),
    // 重定向到角色列表
    redirect: '/mainCont',
    children: [
      {
        path: '/log',
        name: 'log',
        component: (resolve) => require(['../views/log.vue'], resolve)
      },
      {
        path: '/substance',
        name: 'substance',
        component: (resolve) => require(['../views/workOrder/substance.vue'], resolve)
      },
      {
        path: '/list',
        name: 'shouye',
        component: (resolve) => require(['../views/shouye.vue'], resolve)
      },
      {
        path: '/ipPool',
        name: 'ipPool',
        component: (resolve) => require(['../views/ipPool.vue'], resolve)
      },
      {
        path: '/jkptindex',
        component: (resolve) => require(['../views/jkpt/mainCont.vue'], resolve),
        name: 'jkptindex',
        // redirect: '/repositorySearch',
        children: [
          {
            path: '/jkiframe/:id',
            component: (resolve) => require(['../views/jkpt/jkiframe.vue'], resolve),
            name: 'jkiframe'
          }
        ]
      },
      {
        path: '/help',
        name: 'help',
        component: (resolve) => require(['../views/help.vue'], resolve)
      },
      // 工单
      {
        path: '/mainCont',
        component: (resolve) => require(['../views/workOrder/mainCont.vue'], resolve),
        name: 'mainCont',
        // redirect: '/list',
        children: [
          {
            path: '/list1',
            component: (resolve) => require(['../views/workOrder/list.vue'], resolve),
            name: 'list',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/workorder',
            component: (resolve) => require(['../views/workOrder/workorder.vue'], resolve),
            name: 'workorder',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/callLog',
            component: (resolve) => require(['../views/workOrder/callLog.vue'], resolve),
            name: 'callLog',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/searchorder',
            component: (resolve) => require(['../views/workOrder/searchorder.vue'], resolve),
            name: 'searchorder',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/workapprove',
            component: (resolve) => require(['../views/workOrder/approve.vue'], resolve),
            name: 'workapprove',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/automatic',
            component: (resolve) => require(['../views/workOrder/automatic.vue'], resolve),
            name: 'automatic',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/phrase',
            component: (resolve) => require(['../views/workOrder/set/phrase.vue'], resolve),
            name: 'phrase',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/parameter',
            component: (resolve) => require(['../views/workOrder/set/parameter.vue'], resolve),
            name: 'parameter'
          },
          {
            path: '/timeRange',
            component: (resolve) => require(['../views/workOrder/set/timeRange.vue'], resolve),
            name: 'timeRange',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/workTimeRange',
            component: (resolve) => require(['../views/workOrder/set/workTimeRange.vue'], resolve),
            name: 'workTimeRange',
            meta: {
              keepAlive: true // 需要缓存
            }
          },

          {
            path: '/scheduling',
            component: (resolve) => require(['../views/workOrder/set/scheduling.vue'], resolve),
            name: 'scheduling',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/watchmanScheduling',
            component: (resolve) =>
              require(['../views/workOrder/set/watchmanScheduling.vue'], resolve),
            name: 'watchmanScheduling',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/ipset',
            component: (resolve) => require(['../views/workOrder/set/ipset.vue'], resolve),
            name: 'ipset',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/causes',
            component: (resolve) => require(['../views/workOrder/set/mainCont.vue'], resolve),
            name: 'causes',
            redirect: '/faultCause',
            children: [
              {
                path: '/faultCause',
                component: (resolve) => require(['../views/workOrder/set/faultCause.vue'], resolve),
                name: 'faultCause',
                meta: {
                  keepAlive: true // 需要缓存
                }
              }
            ]
          },
          {
            path: '/types',
            component: (resolve) => require(['../views/workOrder/set/typeMain.vue'], resolve),
            name: 'types',
            redirect: '/faultType',
            children: [
              {
                path: '/faultType',
                component: (resolve) => require(['../views/workOrder/set/faultType.vue'], resolve),
                name: 'faultType',
                meta: {
                  keepAlive: true // 需要缓存
                }
              }
            ]
          },
          {
            path: '/statistical_engineer',
            component: (resolve) =>
              require(['../views/workOrder/statistical/engineer.vue'], resolve),
            name: 'statistical_engineer',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistical_group',
            component: (resolve) => require(['../views/workOrder/statistical/group.vue'], resolve),
            name: 'statistical_group',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistical_department',
            component: (resolve) =>
              require(['../views/workOrder/statistical/department.vue'], resolve),
            name: 'statistical_department',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistical_faultCause',
            component: (resolve) =>
              require(['../views/workOrder/statistical/faultCause.vue'], resolve),
            name: 'statistical_faultCause',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistical_faultType',
            component: (resolve) =>
              require(['../views/workOrder/statistical/faultType.vue'], resolve),
            name: 'statistical_faultType',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistical_score',
            component: (resolve) => require(['../views/workOrder/statistical/score.vue'], resolve),
            name: 'statistical_score',
            meta: {
              keepAlive: true // 需要缓存
            }
          }
        ]
      },
      // 资产
      {
        path: '/mainCont3',
        component: (resolve) => require(['../views/property/mainCont.vue'], resolve),
        name: 'mainCont3',
        // redirect: '/property',
        children: [
          {
            path: '/property',
            component: (resolve) => require(['../views/property/property.vue'], resolve),
            name: 'property'
          },
          {
            path: '/scrap',
            component: (resolve) => require(['../views/property/scrap.vue'], resolve),
            name: 'scrap'
          },
          {
            path: '/scrapApprove',
            component: (resolve) => require(['../views/property/scrapApprove.vue'], resolve),
            name: 'scrapApprove'
          },
          {
            path: '/editApprove',
            component: (resolve) => require(['../views/property/editApprove.vue'], resolve),
            name: 'editApprove'
          },
          {
            path: '/brand',
            component: (resolve) => require(['../views/property/brand.vue'], resolve),
            name: 'brand'
          },
          {
            path: '/assetType',
            component: (resolve) => require(['../views/property/assetType.vue'], resolve),
            name: 'assetType'
          },
          {
            path: '/print',
            component: (resolve) => require(['../views/property/print.vue'], resolve),
            name: 'print'
          }
        ]
      },
      // 巡检
      {
        path: '/mainCont4',
        component: (resolve) => require(['../views/inspection/mainCont.vue'], resolve),
        name: 'mainCont4',
        // redirect: '/unit',
        children: [
          {
            path: '/unit',
            component: (resolve) => require(['../views/inspection/unit.vue'], resolve),
            name: 'unit'
          },
          {
            path: '/assetInspection',
            component: (resolve) => require(['../views/inspection/assetInspection.vue'], resolve),
            name: 'assetInspection'
          },
          {
            path: '/plan',
            component: (resolve) => require(['../views/inspection/plan.vue'], resolve),
            name: 'plan'
          },
          {
            path: '/quest',
            component: (resolve) => require(['../views/inspection/quest.vue'], resolve),
            name: 'quest'
          },
          {
            path: '/record',
            component: (resolve) => require(['../views/inspection/record.vue'], resolve),
            name: 'record'
          },
          {
            path: '/inspect_day',
            component: (resolve) => require(['../views/inspection/statistics/day.vue'], resolve),
            name: 'inspect_day'
          },
          {
            path: '/inspect_peo',
            component: (resolve) => require(['../views/inspection/statistics/people.vue'], resolve),
            name: 'inspect_peo'
          },
          {
            path: '/period',
            component: (resolve) => require(['../views/inspection/set/period.vue'], resolve),
            name: 'period'
          },
          {
            path: '/inspect_type',
            component: (resolve) => require(['../views/inspection/set/type.vue'], resolve),
            name: 'inspect_type'
          }
        ]
      },
      // 合同
      {
        path: '/mainCont5',
        component: (resolve) => require(['../views/contract/mainCont.vue'], resolve),
        name: 'mainCont5',
        // redirect: '/agreement',
        children: [
          {
            path: '/agreement',
            component: (resolve) => require(['../views/contract/agreement.vue'], resolve),
            name: 'agreement'
          },
          {
            path: '/xminvoice',
            component: (resolve) => require(['../views/contract/xminvoice.vue'], resolve),
            name: 'xminvoice'
          },
          {
            path: '/supplier',
            component: (resolve) => require(['../views/contract/supplier.vue'], resolve),
            name: 'supplier'
          },
          {
            path: '/needpay',
            component: (resolve) => require(['../views/project/statistics/needpay.vue'], resolve),
            name: 'needpay'
          },
          // {
          // 	path: '/ispay',
          // 	component: (resolve) => require(['../views/project/statistics/ispay.vue'],
          // 		resolve),
          // 	name: "ispay",

          // },
          {
            path: '/contract',
            component: (resolve) => require(['../views/project/set/contract.vue'], resolve),
            name: 'contract'
          },
          {
            path: '/invoice',
            component: (resolve) => require(['../views/project/set/invoice.vue'], resolve),
            name: 'invoice'
          }
        ]
      },
      // 项目
      {
        path: '/mainCont2',
        component: (resolve) => require(['../views/project/mainCont.vue'], resolve),
        name: 'mainCont2',
        // redirect: '/allProject',
        children: [
          {
            path: '/allProject',
            component: (resolve) => require(['../views/project/project/allProject.vue'], resolve),
            name: 'allProject',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/projectRequirement',
            component: (resolve) =>
              require(['../views/project/statistics/projectRequirement.vue'], resolve),
            name: 'projectRequirement',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/principal',
            component: (resolve) => require(['../views/project/project/principal.vue'], resolve),
            name: 'principal',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/participation',
            component: (resolve) =>
              require(['../views/project/project/participation.vue'], resolve),
            name: 'participation',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/attention',
            component: (resolve) => require(['../views/project/project/attention.vue'], resolve),
            name: 'attention',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/task',
            component: (resolve) => require(['../views/project/task/task.vue'], resolve),
            name: 'task',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/sendMe',
            component: (resolve) => require(['../views/project/task/sendMe.vue'], resolve),
            name: 'sendMe',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/failTask',
            component: (resolve) => require(['../views/project/task/fail.vue'], resolve),
            name: 'failTask',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/myParticipation',
            component: (resolve) => require(['../views/project/task/myParticipation.vue'], resolve),
            name: 'myParticipation',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/arrange',
            component: (resolve) => require(['../views/project/task/arrange.vue'], resolve),
            name: 'arrange',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/overdue',
            component: (resolve) => require(['../views/project/task/overdue.vue'], resolve),
            name: 'overdue',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/completed',
            component: (resolve) => require(['../views/project/task/completed.vue'], resolve),
            name: 'completed',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/foucs',
            component: (resolve) => require(['../views/project/task/foucs.vue'], resolve),
            name: 'foucs',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/dynamic',
            component: (resolve) => require(['../views/project/msg/dynamic.vue'], resolve),
            name: 'dynamic',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/projectDynamics',
            component: (resolve) => require(['../views/project/msg/projectDynamics.vue'], resolve),
            name: 'projectDynamics',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/approve',
            component: (resolve) => require(['../views/project/msg/approve.vue'], resolve),
            name: 'approve',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/unread',
            component: (resolve) => require(['../views/project/msg/unread.vue'], resolve),
            name: 'unread',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/rwTemplate',
            component: (resolve) => require(['../views/project/templates/rwTemplate.vue'], resolve),
            name: 'rwTemplate',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/xmTemplate',
            component: (resolve) => require(['../views/project/templates/xmTemplate.vue'], resolve),
            name: 'xmTemplate',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/statistics',
            component: (resolve) =>
              require(['../views/project/statistics/statistics.vue'], resolve),
            name: 'statistics',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/analysis',
            component: (resolve) => require(['../views/project/statistics/analysis.vue'], resolve),
            name: 'analysis',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/demand',
            component: (resolve) => require(['../views/project/set/demand.vue'], resolve),
            name: 'demand',
            meta: {
              keepAlive: true // 需要缓存
            }
          },
          {
            path: '/conference',
            component: (resolve) => require(['../views/project/set/conference.vue'], resolve),
            name: 'conference',
            meta: {
              keepAlive: true // 需要缓存
            }
          }
        ]
      },
      // 知识库
      {
        path: '/repository',
        component: (resolve) => require(['../views/repository/mainCont.vue'], resolve),
        name: 'repository',
        // redirect: '/repositorySearch',
        children: [
          {
            path: '/internet',
            component: (resolve) => require(['../views/repository/internet.vue'], resolve),
            name: 'internet'
          },
          {
            path: '/repositorySearch',
            component: (resolve) => require(['../views/repository/search.vue'], resolve),
            name: 'repositorySearch'
          },
          {
            path: '/myRepository',
            component: (resolve) => require(['../views/repository/myRepository.vue'], resolve),
            name: 'myRepository'
          },
          {
            path: '/repositoryAudit',
            component: (resolve) => require(['../views/repository/repositoryAudit.vue'], resolve),
            name: 'repositoryAudit'
          }
        ]
      },
      // 配置
      {
        path: '/allocation',
        component: (resolve) => require(['../views/allocation/mainCont.vue'], resolve),
        name: 'allocation',
        // redirect: '/department',
        children: [
          {
            path: '/depart',
            component: (resolve) =>
              require(['../views/allocation/department/mainCont.vue'], resolve),
            name: 'depart',
            redirect: '/department',
            children: [
              {
                path: '/department',
                component: (resolve) =>
                  require(['../views/allocation/department/department.vue'], resolve),
                name: 'department'
              }
            ]
          },
          {
            path: '/user',
            component: (resolve) => require(['../views/allocation/user/mainCont.vue'], resolve),
            name: 'user',
            redirect: '/userlist',
            children: [
              {
                path: '/userlist',
                component: (resolve) => require(['../views/allocation/user/user.vue'], resolve),
                name: 'userlist'
              }
            ]
          },
          {
            path: '/group',
            component: (resolve) => require(['../views/allocation/group.vue'], resolve),
            name: 'group'
          },
          {
            path: '/engineer',
            component: (resolve) => require(['../views/allocation/engineer.vue'], resolve),
            name: 'engineer'
          },
          {
            path: '/permissions',
            component: (resolve) => require(['../views/allocation/permissions.vue'], resolve),
            name: 'permissions'
          },
          {
            path: '/subsystem',
            component: (resolve) => require(['../views/allocation/subsystem.vue'], resolve),
            name: 'subsystem'
          },
          {
            path: '/three',
            component: (resolve) => require(['../views/allocation/three.vue'], resolve),
            name: 'three'
          },
          {
            path: '/account',
            component: (resolve) => require(['../views/allocation/account.vue'], resolve),
            name: 'account'
          },
          {
            path: '/params',
            component: (resolve) => require(['../views/allocation/params.vue'], resolve),
            name: 'params'
          },
          {
            path: '/phone',
            component: (resolve) => require(['../views/allocation/phone.vue'], resolve),
            name: 'phone'
          },
          // -------新增  配置页面中新增两个页面
          {
            path: '/setting',
            component: (resolve) =>
              require(['../views/allocation/department/setting.vue'], resolve),
            name: 'setting'
          },
          {
            path: '/clearjour',
            component: (resolve) =>
              require(['../views/allocation/department/clearJour.vue'], resolve),
            name: 'clearjour'
            // meta: {
            // 	keepAlive: true, // 需要缓存  修改提交时提交数据时也获取数据  否则保持数据
            // }
          }
          // -------
        ]
      }
    ]
  }
];
const router = new VueRouter({
  routes
});

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
//解决vue路由重复导航错误
//获取原型对象上的push函数

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //如果用户访问的是登录页面，则直接放行
  if (to.path === '/login' || to.path === '/ding' || to.path === '/reg') {
    next();
  } else {
    //从 sessionStorage 中获取 保存的 token
    const tokenStr = sessionStorage.getItem('token');
    // console.log(tokenStr)
    //如果没有token，强制跳转到登录页面，如果有，则直接放行
    if (iftest) {
      next(); //测试环境  不需要登录
    } else {
      if (!tokenStr || tokenStr == 'undefined') {
        next('/login');
      } else {
        next(); //正式环境必须登录  否则无法进入
      }
    }
    // if (!tokenStr || tokenStr == 'undefined') {
    // 	next('/login')
    // } else {
    // 	next()
    // }
  }
});

export default router;
